﻿using Abp.Domain.Entities;
using GN.Pay.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GN.Pay.Core.Entities
{
    //付款申请
    [Table("Trade_PayApply")]
    [Serializable]
    public class TradePayApply : Entity<string>
    {
        /// <summary>
        /// 应用Id
        /// </summary>
        [Required]
        [StringLength(maximumLength: 36)]
        public string AppId { get; set; }

        /// <summary>
        /// 订单号
        /// </summary>
        [Required]
        [StringLength(maximumLength: 32)]
        public string OrderNo { get; set; }

        /// <summary>
        /// 订单时间
        /// </summary>
        public DateTime OrderTime { get; set; }

        /// <summary>
        /// 订单金额
        /// </summary>
        public decimal OrderMoney { get; set; }

        /// <summary>
        /// 会员编号
        /// </summary>
        [StringLength(maximumLength: 64)]
        public string MemberNo { get; set; }

        /// <summary>
        /// 获取或设置用户标识(如微信 openId等)
        /// </summary>    
        [MaxLength(255, ErrorMessage = "用户标识不能超过 255 位的长度 ")]
        public string UserToken { get; set; }

        /// <summary>
        /// 商品标题
        /// </summary>
        [Required]
        public string Subject { get; set; }

        /// <summary>
        /// 商品详情
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// 订单备注
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 应用Ip地址
        /// </summary>
        public string AppIp { get; set; }

        /// <summary>
        /// 过期分钟
        /// </summary>
        [Range(0, int.MaxValue)]
        public int ExpireMinute { get; set; }

        /// <summary>
        /// 通知地址(Post)
        /// </summary>
        [StringLength(maximumLength: 255)]
        public string NotifyUrl { get; set; }

        /// <summary>
        /// 返回地址(Get)
        /// </summary>
        [StringLength(maximumLength: 255)]
        public string ReturnUrl { get; set; }       

        /// <summary>
        /// 扩展参数
        /// </summary>
        public string ExtraParam { get; set; }

        /// <summary>
        /// 申请平台数量
        /// </summary>
        [Range(0, int.MaxValue)]
        public int ApplyPlatformCount { get; set; }

        /// <summary>
        /// 申请时间
        /// </summary>
        public DateTime ApplyTime { get; set; }

        /// <summary>
        /// 付款申请通道
        /// </summary>
        [ForeignKey("ApplyId")]
        public virtual ICollection<TradePayApplyChannel> TradePayApplyChannels { get; set; }

        /// <summary>
        /// 订单检查
        /// </summary>
        public void Check()
        {
            if (string.IsNullOrWhiteSpace(OrderNo)
                || this.OrderNo.Contains("_"))
            {
                PayExceptionUtils.ThrowPayValidationException("无效的订单号，不能为 null、空白，也不能包含下划线。");
            }
            OrderMoney = Math.Round(OrderMoney, 2);
        }

        /// <summary>
        /// 是否过期
        /// </summary>
        /// <returns></returns>
        public bool IsExpire()
        {
            return ApplyTime.AddMinutes(ExpireMinute) < DateTime.Now;
        }
    }
}
